<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
    <head><meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'><meta name='description' content='MySQL &#43; Keepalived 双主热备高可用操作记录 我们通常说的双机热备是指两台机器都在运行，但并不是两台机器都同时在提供服务。当提供服务的一台出现故障的时候，另外'>
<title>Keepalived</title>

<link rel='canonical' href='/p/keepalived/'>

<link rel="stylesheet" href="/scss/style.min.56b4b4efdee21dd7cd9a6ec6cdb1882f71985765df9c7591a90716c58b48f4c5.css"><meta property='og:title' content='Keepalived'>
<meta property='og:description' content='MySQL &#43; Keepalived 双主热备高可用操作记录 我们通常说的双机热备是指两台机器都在运行，但并不是两台机器都同时在提供服务。当提供服务的一台出现故障的时候，另外'>
<meta property='og:url' content='/p/keepalived/'>
<meta property='og:site_name' content='良宏'>
<meta property='og:type' content='article'><meta property='article:section' content='Post' /><meta property='article:tag' content='keepalived' /><meta property='article:published_time' content='2022-09-16T08:09:21&#43;08:00'/><meta property='article:modified_time' content='2022-09-16T08:09:21&#43;08:00'/>
<meta name="twitter:title" content="Keepalived">
<meta name="twitter:description" content="MySQL &#43; Keepalived 双主热备高可用操作记录 我们通常说的双机热备是指两台机器都在运行，但并不是两台机器都同时在提供服务。当提供服务的一台出现故障的时候，另外">
    </head>
    <body class="
    article-page
    ">
    <script>
        (function() {
            const colorSchemeKey = 'StackColorScheme';
            if(!localStorage.getItem(colorSchemeKey)){
                localStorage.setItem(colorSchemeKey, "auto");
            }
        })();
    </script><script>
    (function() {
        const colorSchemeKey = 'StackColorScheme';
        const colorSchemeItem = localStorage.getItem(colorSchemeKey);
        const supportDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;

        if (colorSchemeItem == 'dark' || colorSchemeItem === 'auto' && supportDarkMode) {
            

            document.documentElement.dataset.scheme = 'dark';
        } else {
            document.documentElement.dataset.scheme = 'light';
        }
    })();
</script>
<div class="container main-container flex on-phone--column extended"><aside class="sidebar left-sidebar sticky ">
    <button class="hamburger hamburger--spin" type="button" id="toggle-menu" aria-label="切换菜单">
        <span class="hamburger-box">
            <span class="hamburger-inner"></span>
        </span>
    </button>

    <header>
        
            
            <figure class="site-avatar">
                <a href="/">
                
                    
                    
                    
                        
                        <img src="/img/wx_150x150_hub08581134c092243b209205c3073808c_6075_300x0_resize_q75_box.jpeg" width="300"
                            height="300" class="site-logo" loading="lazy" alt="Avatar">
                    
                
                </a>
                
                    <span class="emoji">🍥</span>
                
            </figure>
            
        
        
        <div class="site-meta">
            <h1 class="site-name"><a href="/">良宏</a></h1>
            <h2 class="site-description">好好学习，天天向上。</h2>
        </div>
    </header><ol class="menu" id="main-menu">
        
        
        
        <li >
            <a href='/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-home" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <polyline points="5 12 3 12 12 3 21 12 19 12" />
  <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" />
  <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" />
</svg>



                
                <span>Home</span>
            </a>
        </li>
        
        
        <li >
            <a href='/about/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="7" r="4" />
  <path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2" />
</svg>



                
                <span>About</span>
            </a>
        </li>
        
        
        <li >
            <a href='/archives/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-archive" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <rect x="3" y="4" width="18" height="4" rx="2" />
  <path d="M5 8v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-10" />
  <line x1="10" y1="12" x2="14" y2="12" />
</svg>



                
                <span>Archives</span>
            </a>
        </li>
        
        
        <li >
            <a href='/search/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-search" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="10" cy="10" r="7" />
  <line x1="21" y1="21" x2="15" y2="15" />
</svg>



                
                <span>Search</span>
            </a>
        </li>
        

        <div class="menu-bottom-section">
            
            
                <li id="dark-mode-toggle">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-left" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="8" cy="12" r="2" />
  <rect x="2" y="6" width="20" height="12" rx="6" />
</svg>



                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-right" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="16" cy="12" r="2" />
  <rect x="2" y="6" width="20" height="12" rx="6" />
</svg>



                    <span>暗色模式</span>
                </li>
            
        </div>
    </ol>
</aside>

    

            <main class="main full-width">
    <article class="main-article">
    <header class="article-header">

    <div class="article-details">
    
    <header class="article-category">
        
            <a href="/categories/tech/" style="background-color: blueviolet; color: #fff;">
                Tech
            </a>
        
            <a href="/categories/software/" style="background-color: #6b69d6; color: #fff;">
                Software
            </a>
        
            <a href="/categories/tool/" style="background-color: #0177b8; color: #fff;">
                Tool
            </a>
        
    </header>
    

    <div class="article-title-wrapper">
        <h2 class="article-title">
            <a href="/p/keepalived/">Keepalived</a>
        </h2>
    
        
    </div>

    
    
    
    
    <footer class="article-time">
        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-calendar-time" width="56" height="56" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M11.795 21h-6.795a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v4" />
  <circle cx="18" cy="18" r="4" />
  <path d="M15 3v4" />
  <path d="M7 3v4" />
  <path d="M3 11h16" />
  <path d="M18 16.496v1.504l1 1" />
</svg>
                <time class="article-time--published">Sep 16, 2022</time>
            </div>
        

        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clock" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <polyline points="12 7 12 12 15 15" />
</svg>



                <time class="article-time--reading">
                    阅读时长: 17 分钟
                </time>
            </div>
        
    </footer>
    

    
</div>

</header>

    <section class="article-content">
    
    
    <h3 id="keepalived安装与配置">keepalived安装与配置</h3>
<h4 id="httpsmakefileso20220505install-keepalived前言-前言前言"><a class="link" href="https://makefile.so/2022/05/05/install-keepalived//#%e5%89%8d%e8%a8%80"  title="前言"
     target="_blank" rel="noopener"
    ></a>前言</h4>
<p>在多机环境下，安装keepalived，保证高可用；<br>
同时多个机器对外暴露一个VIP（Virtual IP），客户端连接VIP即可；</p>
<h4 id="httpsmakefileso20220505install-keepalived开始-开始开始"><a class="link" href="https://makefile.so/2022/05/05/install-keepalived//#%e5%bc%80%e5%a7%8b"  title="开始"
     target="_blank" rel="noopener"
    ></a>开始</h4>
<h5 id="httpsmakefileso20220505install-keepalived0-前置条件-0-前置条件0-前置条件"><a class="link" href="https://makefile.so/2022/05/05/install-keepalived//#0-%e5%89%8d%e7%bd%ae%e6%9d%a1%e4%bb%b6"  title="0. 前置条件"
     target="_blank" rel="noopener"
    ></a>0. 前置条件</h5>
<ol>
<li>多个机器，如：
<ol>
<li>Node1（主节点 MASTER）： 192.168.31.1</li>
<li>Node2（备用节点 SLAVE）： 192.168.31.2</li>
</ol>
</li>
<li>root权限；</li>
</ol>
<h5 id="httpsmakefileso20220505install-keepalived1-安装-1-安装1-安装"><a class="link" href="https://makefile.so/2022/05/05/install-keepalived//#1-%e5%ae%89%e8%a3%85"  title="1. 安装"
     target="_blank" rel="noopener"
    ></a>1. 安装</h5>
<p>在每个机器上安装keepalived：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">   sudo apt install keepalived -y
</span></span><span class="line"><span class="cl">   systemctl <span class="nb">enable</span> keepalived
</span></span><span class="line"><span class="cl">   systemctl start keepalived
</span></span></code></pre></div><h5 id="httpsmakefileso20220505install-keepalived2-配置-2-配置2-配置"><a class="link" href="https://makefile.so/2022/05/05/install-keepalived//#2-%e9%85%8d%e7%bd%ae"  title="2. 配置"
     target="_blank" rel="noopener"
    ></a>2. 配置</h5>
<ol>
<li>
<p>默认是没有配置文件的，需要手动添加并修改：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">cd</span> /etc/keepalived
</span></span><span class="line"><span class="cl">touch keepalived.conf
</span></span></code></pre></div></li>
<li>
<p>主节点的<code>keepalived.conf</code>内容如下：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="c1">#检查应用是否正常的script：</span>
</span></span><span class="line"><span class="cl">vrrp_script chk_service_ok <span class="o">{</span>
</span></span><span class="line"><span class="cl">       <span class="c1">#这里只检查/tmp/service_ok这个文件是否存在：</span>
</span></span><span class="line"><span class="cl">       script <span class="s2">&#34;ls /tmp/service_ok&#34;</span>
</span></span><span class="line"><span class="cl">       <span class="c1">#间隔2秒检查一次：</span>
</span></span><span class="line"><span class="cl">       interval <span class="m">2</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">vrrp_instance VI_1 <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">#指定网卡名称；可以通过：ip addr命令查看网卡信息：</span>
</span></span><span class="line"><span class="cl">    interface enp3s0
</span></span><span class="line"><span class="cl">    <span class="c1">#主节点：</span>
</span></span><span class="line"><span class="cl">    state MASTER
</span></span><span class="line"><span class="cl">    <span class="c1">#虚拟路由ID，主节点、备用节点必须为通一个ID：</span>
</span></span><span class="line"><span class="cl">    virtual_router_id <span class="m">51</span>
</span></span><span class="line"><span class="cl">    <span class="c1">#优先级；主节点比备用节点高：</span>
</span></span><span class="line"><span class="cl">    priority <span class="m">100</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">#VIP：</span>
</span></span><span class="line"><span class="cl">    virtual_ipaddress <span class="o">{</span>
</span></span><span class="line"><span class="cl">        192.168.31.20/25
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">#上文指定的检查脚本：</span>
</span></span><span class="line"><span class="cl">    track_script <span class="o">{</span>
</span></span><span class="line"><span class="cl">       chk_service_ok
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></div></li>
<li>
<p>备用节点的<code>keepalived.conf</code>内容如下：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="c1">#检查应用是否正常的script：</span>
</span></span><span class="line"><span class="cl">vrrp_script chk_service_ok <span class="o">{</span>
</span></span><span class="line"><span class="cl">       <span class="c1">#这里只检查/tmp/service_ok这个文件是否存在：</span>
</span></span><span class="line"><span class="cl">       script <span class="s2">&#34;ls /tmp/service_ok&#34;</span>
</span></span><span class="line"><span class="cl">       <span class="c1">#间隔2秒检查一次：</span>
</span></span><span class="line"><span class="cl">       interval <span class="m">2</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">vrrp_instance VI_1 <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">#指定网卡名称；可以通过：ip addr命令查看网卡信息：</span>
</span></span><span class="line"><span class="cl">    interface enp3s0
</span></span><span class="line"><span class="cl">    <span class="c1">#备用节点：</span>
</span></span><span class="line"><span class="cl">    state SLAVE
</span></span><span class="line"><span class="cl">    <span class="c1">#虚拟路由ID，主节点、备用节点必须为通一个ID：</span>
</span></span><span class="line"><span class="cl">    virtual_router_id <span class="m">51</span>
</span></span><span class="line"><span class="cl">    <span class="c1">#优先级；主节点比备用节点高：</span>
</span></span><span class="line"><span class="cl">    priority <span class="m">50</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">#VIP：</span>
</span></span><span class="line"><span class="cl">    virtual_ipaddress <span class="o">{</span>
</span></span><span class="line"><span class="cl">        192.168.31.20/25
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">#上文指定的检查脚本：</span>
</span></span><span class="line"><span class="cl">    track_script <span class="o">{</span>
</span></span><span class="line"><span class="cl">       chk_service_ok
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></div></li>
<li>
<p>在主节点和备用节点同时创建<code>/tmp/service_ok</code>文件：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">touch /tmp/service_ok
</span></span></code></pre></div></li>
<li>
<p>重启keepalived：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">systemctl restart keepalived
</span></span></code></pre></div></li>
<li>
<p>测试：使用ssh连接虚拟IP：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">ssh root@192.168.31.20
</span></span><span class="line"><span class="cl"><span class="c1">#此时连接的是主节点192.168.31.1；</span>
</span></span><span class="line"><span class="cl"><span class="c1">#删除主节点的/tmp/service_ok文件后，</span>
</span></span><span class="line"><span class="cl"><span class="c1">#ssh连接会断开；</span>
</span></span><span class="line"><span class="cl"><span class="c1">#再次连接后，连接的是备用节点192.168.31.2；</span>
</span></span><span class="line"><span class="cl"><span class="c1">#再次创建主节点的/tmp/service_ok文件后，</span>
</span></span><span class="line"><span class="cl"><span class="c1">#ssh会断开，再次连接后，恢复为主节点；</span>
</span></span></code></pre></div></li>
</ol>
<h5 id="httpsmakefileso20220505install-keepalived3-配置多个要监听的应用-3-配置多个要监听的应用3-配置多个要监听的应用"><a class="link" href="https://makefile.so/2022/05/05/install-keepalived//#3-%e9%85%8d%e7%bd%ae%e5%a4%9a%e4%b8%aa%e8%a6%81%e7%9b%91%e5%90%ac%e7%9a%84%e5%ba%94%e7%94%a8"  title="3. 配置多个要监听的应用"
     target="_blank" rel="noopener"
    ></a>3. 配置多个要监听的应用</h5>
<p>配置多个要监听的应用，只需添加多个IP和检查脚本；<br>
如下为Nginx监听脚本：<br>
主节点：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">   vrrp_script chk_service_ok <span class="o">{</span>
</span></span><span class="line"><span class="cl">       script <span class="s2">&#34;ls /tmp/service_ok&#34;</span>
</span></span><span class="line"><span class="cl">       interval <span class="m">2</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">vrrp_script chk_nginx_ok <span class="o">{</span>
</span></span><span class="line"><span class="cl">       script <span class="s2">&#34;/etc/keepalived/chk_nginx.sh&#34;</span>
</span></span><span class="line"><span class="cl">       interval <span class="m">2</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">vrrp_instance VI_1 <span class="o">{</span>
</span></span><span class="line"><span class="cl">    interface enp5s0
</span></span><span class="line"><span class="cl">    state MASTER
</span></span><span class="line"><span class="cl">    virtual_router_id <span class="m">51</span>
</span></span><span class="line"><span class="cl">    priority <span class="m">100</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    virtual_ipaddress <span class="o">{</span>
</span></span><span class="line"><span class="cl">        192.168.31.20/25
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    track_script <span class="o">{</span>
</span></span><span class="line"><span class="cl">       chk_service_ok
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">vrrp_instance VI_2 <span class="o">{</span>
</span></span><span class="line"><span class="cl">    interface enp5s0
</span></span><span class="line"><span class="cl">    state MASTER
</span></span><span class="line"><span class="cl">    virtual_router_id <span class="m">52</span>
</span></span><span class="line"><span class="cl">    priority <span class="m">100</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    virtual_ipaddress <span class="o">{</span>
</span></span><span class="line"><span class="cl">        192.168.31.21/25
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    track_script <span class="o">{</span>
</span></span><span class="line"><span class="cl">       chk_nginx_ok
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></div><p>备用节点：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">   vrrp_script chk_service_ok <span class="o">{</span>
</span></span><span class="line"><span class="cl">       script <span class="s2">&#34;ls /tmp/service_ok&#34;</span>
</span></span><span class="line"><span class="cl">       interval <span class="m">2</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">vrrp_script chk_nginx_ok <span class="o">{</span>
</span></span><span class="line"><span class="cl">      script <span class="s2">&#34;/etc/keepalived/chk_nginx.sh&#34;</span>
</span></span><span class="line"><span class="cl">       interval <span class="m">2</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">vrrp_instance VI_1 <span class="o">{</span>
</span></span><span class="line"><span class="cl">    interface enp3s0
</span></span><span class="line"><span class="cl">    state BACKUP
</span></span><span class="line"><span class="cl">    virtual_router_id <span class="m">51</span>
</span></span><span class="line"><span class="cl">    priority <span class="m">50</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    virtual_ipaddress <span class="o">{</span>
</span></span><span class="line"><span class="cl">        192.168.31.20/25
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    track_script <span class="o">{</span>
</span></span><span class="line"><span class="cl">       chk_service_ok
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">vrrp_instance VI_2 <span class="o">{</span>
</span></span><span class="line"><span class="cl">    interface enp3s0
</span></span><span class="line"><span class="cl">    state BACKUP
</span></span><span class="line"><span class="cl">    virtual_router_id <span class="m">52</span>
</span></span><span class="line"><span class="cl">    priority <span class="m">50</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    virtual_ipaddress <span class="o">{</span>
</span></span><span class="line"><span class="cl">        192.168.31.21/25
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    track_script <span class="o">{</span>
</span></span><span class="line"><span class="cl">       chk_nginx_ok
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></div><p><code>chk_nginx.sh</code>内容如下：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">   <span class="c1">#!/bin/bash</span>
</span></span><span class="line"><span class="cl">   <span class="nb">set</span> -ex
</span></span><span class="line"><span class="cl">   
</span></span><span class="line"><span class="cl">   <span class="k">if</span> <span class="o">[</span> -z <span class="s2">&#34;`pidof nginx`&#34;</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
</span></span><span class="line"><span class="cl">     <span class="nb">exit</span> <span class="m">1</span>
</span></span><span class="line"><span class="cl">   <span class="k">fi</span>
</span></span></code></pre></div><h5 id="httpsmakefileso20220505install-keepalived4-测试-4-测试4-测试"><a class="link" href="https://makefile.so/2022/05/05/install-keepalived//#4-%e6%b5%8b%e8%af%95"  title="4. 测试"
     target="_blank" rel="noopener"
    ></a>4. 测试</h5>
<p>客户端使用VIP：<code>192.168.31.21</code>访问Nginx应用：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">   <span class="c1">#Nginx端口为8080：</span>
</span></span><span class="line"><span class="cl">   curl 192.168.31.21:8080
</span></span><span class="line"><span class="cl">   <span class="c1">#此时访问的是主节点；</span>
</span></span><span class="line"><span class="cl">   <span class="c1">#关闭主节点的Nginx：</span>
</span></span><span class="line"><span class="cl">   systemctl stop nginx
</span></span><span class="line"><span class="cl">   <span class="c1">#再次访问：</span>
</span></span><span class="line"><span class="cl">   curl 192.168.31.21:8080
</span></span><span class="line"><span class="cl">   <span class="c1">#此时访问的是备用节点；</span>
</span></span></code></pre></div><h5 id="httpsmakefileso20220505install-keepalived5-查看日志和配置-5-查看日志和配置5-查看日志和配置"><a class="link" href="https://makefile.so/2022/05/05/install-keepalived//#5-%e6%9f%a5%e7%9c%8b%e6%97%a5%e5%bf%97%e5%92%8c%e9%85%8d%e7%bd%ae"  title="5. 查看日志和配置"
     target="_blank" rel="noopener"
    ></a>5. 查看日志和配置</h5>
<ol>
<li>
<p>默认日志路径：<code>/var/log/messages</code>；</p>
</li>
<li>
<p>修改日志级别和路径： <a class="link" href="https://www.cnblogs.com/i0day/p/6070575.html"  target="_blank" rel="noopener"
    >Keepalived日志路径</a>；</p>
</li>
<li>
<p>默认全局配置：<code>/etc/sysconfig/keepalived</code>；</p>
</li>
<li>
<p>默认全局配置文件内容(如果不存在该文件，需要手动创建；cat /etc/sysconfig/keepalived)：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Options for keepalived. keepalived--help &#39; Output and keepalived (8) and</span>
</span></span><span class="line"><span class="cl"><span class="c1"># keepalived.conf (5) Man pages for a list of all options. Here are the most</span>
</span></span><span class="line"><span class="cl"><span class="c1"># common ones:</span>
</span></span><span class="line"><span class="cl"><span class="c1">#</span>
</span></span><span class="line"><span class="cl">--VRRP               -P only    run with VRRP subsystem.
</span></span><span class="line"><span class="cl"><span class="c1">#--check              -C is    run with Health-checker subsystem.</span>
</span></span><span class="line"><span class="cl"><span class="c1">#--DONT-RELEASE-VRRP    -v dont remove VRRP VIPs &amp; vroutes on daemon stop.</span>
</span></span><span class="line"><span class="cl"><span class="c1">#--dont-release-ipvs    -i dont remove Ipvs topology on daemon stop.</span>
</span></span><span class="line"><span class="cl"><span class="c1">#--dump-conf    -D dump the configuration data.</span>
</span></span><span class="line"><span class="cl"><span class="c1">#--log-detail    -D Detailed log messages.</span>
</span></span><span class="line"><span class="cl"><span class="c1">#--log-facility    -S 0-7 Set local syslog facility (Default=log_daemon)</span>
</span></span><span class="line"><span class="cl"><span class="c1">#</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nv">keepalived_options</span><span class="o">=</span> <span class="s1">&#39;-d &#39;</span>
</span></span></code></pre></div></li>
</ol>
<h4 id="httpsmakefileso20220505install-keepalived参考链接-参考链接参考链接"><a class="link" href="https://makefile.so/2022/05/05/install-keepalived//#%e5%8f%82%e8%80%83%e9%93%be%e6%8e%a5"  title="参考链接"
     target="_blank" rel="noopener"
    ></a>参考链接</h4>
<ol>
<li><a class="link" href="https://www.cnblogs.com/kevingrace/p/6710136.html"  target="_blank" rel="noopener"
    >MySQL + Keepalived 双主热备高可用操作记录</a></li>
<li><a class="link" href="https://www.cnblogs.com/lijiaman/p/13430668.html"  target="_blank" rel="noopener"
    >keepalived+MySQL实现高可用</a></li>
<li><a class="link" href="https://www.cnblogs.com/i0day/p/6070575.html"  target="_blank" rel="noopener"
    >修改日志级别和路径</a></li>
</ol>

</section>


    <footer class="article-footer">
    
    <section class="article-tags">
        
            <a href="/tags/keepalived/">keepalived</a>
        
    </section>


    </footer>


    
</article>

    

    

<aside class="related-content--wrapper">
    <h2 class="section-title">相关文章</h2>
    <div class="related-content">
        <div class="flex article-list--tile">
            
                
<article class="">
    <a href="/p/keepalived/">
        
        

        <div class="article-details">
            <h2 class="article-title">Keepalived</h2>
        </div>
    </a>
</article>

            
                
<article class="">
    <a href="/p/%E5%9C%A8-chrome-%E4%B8%AD%E6%A8%A1%E6%8B%9F%E5%BE%AE%E4%BF%A1%E5%86%85%E7%BD%AE%E6%B5%8F%E8%A7%88%E5%99%A8/">
        
        

        <div class="article-details">
            <h2 class="article-title">在 Chrome 中模拟微信内置浏览器</h2>
        </div>
    </a>
</article>

            
                
<article class="">
    <a href="/p/alpine-install/">
        
        

        <div class="article-details">
            <h2 class="article-title">Alpine Install</h2>
        </div>
    </a>
</article>

            
                
<article class="">
    <a href="/p/go-callvis/">
        
        

        <div class="article-details">
            <h2 class="article-title">go-callvis</h2>
        </div>
    </a>
</article>

            
                
<article class="">
    <a href="/p/netstat/">
        
        

        <div class="article-details">
            <h2 class="article-title">Netstat</h2>
        </div>
    </a>
</article>

            
        </div>
    </div>
</aside>

     
    
        
    

    <footer class="site-footer">
    <section class="copyright">
        &copy; 
        
            2021 - 
        
        2023 良宏
    </section>
    <section class="powerby">
        Built with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a>,
        主题 <b><a href="https://github.com/CaiJimmy/hugo-theme-stack" target="_blank" rel="noopener" data-version="3.5.0">Stack</a></b> 由 <a href="https://jimmycai.com" target="_blank" rel="noopener">Jimmy</a> 设计
    </section>
</footer>


    
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    
    <div class="pswp__bg"></div>

    
    <div class="pswp__scroll-wrap">

        
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                
                
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"integrity="sha256-ePwmChbbvXbsO02lbM3HoHbSHTHFAeChekF1xKJdleo="crossorigin="anonymous"
                defer
                >
            </script><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"integrity="sha256-UKkzOn/w1mBxRmLLGrSeyB4e1xbrp4xylgAWb3M42pU="crossorigin="anonymous"
                defer
                >
            </script><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css"crossorigin="anonymous"
            ><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css"crossorigin="anonymous"
            >

            </main>
        </div>
        <script 
                src="https://cdn.jsdelivr.net/npm/node-vibrant@3.1.6/dist/vibrant.min.js"integrity="sha256-awcR2jno4kI5X0zL8ex0vi2z&#43;KMkF24hUW8WePSA9HM="crossorigin="anonymous"
                
                >
            </script><script type="text/javascript" src="/ts/main.js" defer></script><script type="text/javascript" src="/ts/custom.js" defer></script>
<script>
    (function () {
        const customFont = document.createElement('link');
        customFont.href = "https://fonts.font.im/css2?family=Lato:wght@300;400;700&display=swap";

        customFont.type = "text/css";
        customFont.rel = "stylesheet";

        
    }());
</script>


    </body>
</html>
